package com.study.hive.udf;

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

/**
 * 插件性质的开发：
 *      1.实现接口或继承类
 *      2.重写相应的方法
 *      3.打包
 */

/**
 * 自定义UDF函数类
 *  继承hive提提供的GenericUDF类
 *
 */
public class CalStringLengthUDF extends GenericUDF{
    /**
     * 初始化方法
     * @param objectInspectors 传入到函数中的参数对应的类型的鉴别器对象
     * @return 指定函数的返回值类型对应的鉴别器对象
     * @throws UDFArgumentException
     */
    public ObjectInspector initialize(ObjectInspector[] objectInspectors) throws UDFArgumentException {
        //1.校验函数参数的个数
        if (objectInspectors==null || objectInspectors.length!=1){
            throw new UDFArgumentLengthException("函数参数个数不正确！！！");
        }
        //2.校验函数参数类型
        if (!objectInspectors[0].getCategory().equals(ObjectInspector.Category.PRIMITIVE)) {
            throw new UDFArgumentTypeException(0,"函数参数类型不正确！！！");
        }
        //3.返回函数的返回值类型对应的鉴别器对象
        return PrimitiveObjectInspectorFactory.javaIntObjectInspector;
    }

    /**
     * 函数核心处理方法
     * @param deferredObjects 传入函数的参数
     * @return
     * @throws HiveException 函数的返回值
     */
    public Object evaluate(DeferredObject[] deferredObjects) throws HiveException {
        //1. 参数获取
        Object argument = deferredObjects[0].get();
        if (argument==null){
            return 0;
        }
        return argument.toString().length();
    }

    public String getDisplayString(String[] strings) {
        return "";
    }
}
